// pages/mapList/mapAdd/mapAdd.js
const serverUrl = require('../../../config/config.js').serverUrl;
const qiniuUploader = require("../../../utils/qiniuUploader");
//index.js


var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      lat : null,
      lng : null,
      title : null,
    imageUrl: null,
    token:null
  },

submit: function(){
  console.log("lat: ", this.data.lat );
  console.log("lng: " , this.data.lng);
  console.log("title: ", this.data.title);

  var _this = this;
  wx.request({
    url: serverUrl + '/mp/locationInfo/insert', //上线的话必须是https
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT 
    header: {
      'Content-Type': "application/x-www-form-urlencoded;charset=utf-8",
      'Cookie': 'SESSION=' + wx.getStorageSync("sessionId")
    }, // 设置请求的 header
    data: {
      locationName: _this.data.title,
      latitude: _this.data.lat,
      longitude: _this.data.lng
    },
    success: function (res) {
      var success = res.data.success;
      if(success){
      wx.showToast({
        title: '请求数据成功',
        icon: 'success',
        duration: 2000
      })
      }else{
        wx.showToast({
          title: '请求数据失败',
          icon: 'fail',
          duration: 2000
        })
      }

    },
    fail: function () {
      app.consoleLog("请求数据失败");
    },
    complete: function () {
      // complete 
    }
  })
},
  bindInput : function(e){
    this.setData({
      title : e.detail.value
    })
  },

  getLocation: function (e) {
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
         console.log(res);
        that.setData({
          lat : res.latitude,
          lng : res.longitude
        });
        
      }
    })
  },

  getToken() {
    var that = this;
    wx.request({
      url: serverUrl + '/system/getQiNiuUploadToken',//上线的话必须是https
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(res.data.root)
        var data = res.data;
        that.setData({
          token: data.tip
        });
        console.info("res tokens", that.data.token);
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getToken();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
* 图片改变事件
*/
  bindImgChange() {
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数
      success: res => {
        //上传七牛云
        let tempFilePaths = res.tempFilePaths;
        this.uploadQiniu(tempFilePaths);
      }
    });
  },
  /**
  * 图片上传七牛云
  */
  uploadQiniu : function(tempFilePaths) {
    console.info("token",this.data.token);
    let token = this.data.token;
    var that = this;
    wx.uploadFile({
      url: 'https://up-z0.qiniup.com',
      name: 'file',
      filePath: tempFilePaths[0],
      header: {
        "Content-Type": "multipart/form-data"
      },
      formData: {
        token: token,
      },
      success: function (res) {
        let data = JSON.parse(res.data)
        that.setData({
          imageUrl: "http://qiniu.kingrisingsun.xyz/"+data.hash
        });
        console.info("image" + that.data.imageUrl);
        if (res.statusCode == 200){
          
          wx.showToast({
            title: '请求数据成功+' + that.data.imageUrl,
            icon: 'success',
            duration: 2000
          })
        }else{
          wx.showToast({
            title: '请求数据失败',
            icon: 'fail',
            duration: 2000
          })
        }
        console.log('qiniu');
        console.log(res);
        // to do ...
      },
      fail: function (res) {
        console.log(res)
      }
    });
  },

});